//
//  APPErrorView.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2019/2/20.
//  Copyright © 2019 Mr.wang. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class APPErrorHUD: NSObject {
    
    public static let shared = APPErrorHUD()
    
    private let statusLabel = UILabel()
    private let backgroundView = UIView()
    private let contentView = UIView()
    let keyboardViewModel = KeyboardViewModel()
    
    private var isShowing = false
    
    private override init() {
        super.init()
        
        backgroundView.isUserInteractionEnabled = false
        backgroundView.frame = UIScreen.main.bounds
        backgroundView.backgroundColor = .clear
        
        contentView.backgroundColor = UIColor.init(white: 0.1, alpha: 0.9)
        contentView.layer.cornerRadius = 5
        backgroundView.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(backgroundView.snp.centerY).offset(0)
        }
        
        statusLabel.numberOfLines = 0
        statusLabel.font = UIFont.systemFont(ofSize: 14)
        statusLabel.textColor = .white
        statusLabel.preferredMaxLayoutWidth = SCREEN_WIDTH * 0.7
        contentView.addSubview(statusLabel)
        statusLabel.snp.makeConstraints { (make) in
            make.left.top.equalTo(15)
            make.center.equalToSuperview()
        }
        
        keyboardViewModel.keyboardHeightChange
            .subscribe(onNext: { [unowned self] (height, show) in
                guard self.isShowing else { return }
                self.contentView.snp.updateConstraints({ (make) in
                    make.centerY.equalTo(self.backgroundView.snp.centerY).offset(-height * 0.5)
                })
                self.backgroundView.layoutIfNeeded()
            }).disposed(by: rx.disposeBag)
    }
    
    func show(status: String, dismissDelay: Double, complete: @escaping () -> ()) {
        statusLabel.text = status
        if isShowing { return }
        backgroundView.layer.removeAllAnimations()
        contentView.layer.removeAllAnimations()
        
        contentView.snp.updateConstraints({ (make) in
            make.centerY.equalTo(self.backgroundView.snp.centerY).offset(-keyboardViewModel.currentKeyboardHeight * 0.5)
        })
        self.contentView.transform = CGAffineTransform.init(scaleX: 0, y: 0)
        showAnimation(dismissDelay: dismissDelay, complete: complete)
    }
    
    private func showAnimation(dismissDelay: Double, complete: @escaping () -> ()) {
        isShowing = true
        contentView.transform = CGAffineTransform.init(scaleX: 1.4, y: 1.4)
        contentView.alpha = 0
        UIApplication.shared.keyWindow?.addSubview(backgroundView)
        backgroundView.superview?.bringSubviewToFront(backgroundView)
        
        UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 2, options: .curveEaseInOut, animations: {
            self.contentView.alpha = 1
            self.contentView.transform = CGAffineTransform.init(scaleX: 1.0, y: 1.0)
        })
        self.dismissAnimation(delay: dismissDelay, complete: complete)
    }
    
    private func dismissAnimation(delay: Double, complete: @escaping () -> ()) {
        UIView.animate(withDuration: 0.2, delay: delay + 0.2, usingSpringWithDamping: 1, initialSpringVelocity: 2, options: .curveEaseInOut, animations:{
            self.contentView.transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
            self.contentView.alpha = 0
        }) { (_) in
            complete()
            self.isShowing = false
            self.backgroundView.removeFromSuperview()
        }
    }
}
